vimfandomcom-20200223-history
Insert newline without entering insert mode
As you all may know with 'o' or 'O' you can insert a new line after/before the current line. But both commands enter the insert mode, which may sometimes not be what you want. I put this in my vimrc-file to insert a new-line after the current line by pressing Enter (Shift-Enter for inserting a line before the current line): nmap O nmap o If you want to stay in the line where you have been before use the following maps: nmap Oj nmap ok Note: On console versions, the mapping might not work. In those settings, choose any other key you like instead of , for example . If you want to replace the default behavior (using o and O): nnoremap o o nnoremap O O See also *Use Return and Delete keys in normal mode like in insert mode Comments It's also a nice idea to map something like nnoremap ak$ because it's the opposite of Shift-J. A more sophisticated solution would be nnoremap ciW:if match( @", "^\\s*$") < 0exec "norm P-$diw+"endif For whatever reason, I was unable to get the suggest mapping with and to work properly (both ended up inserting below). So I did: nmap o nmap O Which works fine for me. ---- and will work separately in gvim but not in urxvt. See http://stackoverflow.com/questions/598113/can-terminals-detect-shift-enter-or-control-enter ---- Referencing the above, the default terminal on MacOS returns ^M with all modifier keys. If you don't use return to navigate in normal mode, you can remap two returns to insert a new line: :nmap o Since I have inoremap jj in my .vimrc, it take not too much to type ojj or Ojj. ---- To add a blank line below or above and keep the cursor in place, I think this is easiest: nnoremap m`o`` nnoremap m`O`` It uses the jump list to remember the cursor position, so it does clutter up that list. But I don't mind. You could use any other mark, like so (uses mark a): nnoremap mao`a nnoremap maO`a ---- ---- This mapping interferes with command execution in the command-line window. The command-line window can operate in normal or insert mode and in both executes the command from current line. Using nmap instead of map in the mapping declaration partially fixes the problem. Then, to execute the command, you must enter the insert mode and press . While this is a feasible workaround, I prefer to dispense with this mapping and type oii to insert a new line (ii being my map for ). :Excellent point! :map applies to too many modes, I went ahead and made it :nmap. For the other problem, we should edit the mapping to detect the command-line window, like in Syntax folding of Vim scripts. Probably this will need an form of the mapping to do easily. --Fritzophrenic (talk) 15:01, June 27, 2013 (UTC) ---- Just use Tim Pope's excellent vim-unimpaired plugin The plugin provides the following two mappings: [ and ] add newlines before and after the cursor line. https://github.com/tpope/vim-unimpaired ---- If you want to create space where you already have an empty line, yanking the empty line and then paste it. yy - on empty line p - to paste a line below the cursor shift + p - to paste a line above the cursor 5 p - to paste # empty lines below cursor 5 shift + p - to paste # empty lines above cursor note: the number is arbitrary